package edu.fim.presentation.model.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import edu.fim.data.Identifiable;

@Entity
@Table(name = "tb_meals")
public class Meal implements Identifiable<Integer> {

	private static final long serialVersionUID = 7108561654868267073L;
	
	private Integer id;
	private Consumer consumer;
	private List<Portion> portions;
	private Calendar date;
	
	public Meal() {
	}
	
	public Meal(Consumer consumer, Calendar date) {
		this.consumer = consumer;
		this.date = date;
		portions = new ArrayList<>();
	}

	public Meal(Integer id, Consumer consumer, List<Portion> portions, Calendar date) {
		this.id = id;
		this.consumer = consumer;
		this.portions = portions;
		this.date = date;
	}

	@Id
	@Column(name = "id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(targetEntity = Consumer.class, cascade = CascadeType.ALL)
	@JoinColumn(name = "idConsumer")
	public Consumer getConsumer() {
		return consumer;
	}

	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}

	@OneToMany(targetEntity = Portion.class, mappedBy = "meal", cascade = CascadeType.ALL)
	public List<Portion> getPortions() {
		return portions;
	}

	public void setPortions(List<Portion> portions) {
		this.portions = portions;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "time")
	public Calendar getDate() {
		return date;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}
	
	public void add(Portion portion) {
		portions.add(portion);
	}
}